﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using KeRui.Common;
using KeRui.Upload;
using KeRui.TemplateMaker;

public partial class Manage_Cms_SubjectCreate : System.Web.UI.Page
{
    //获得用户自定义数据
    private ManageErrMsg nsb = new ManageErrMsg();
    protected int siteid = PageRequest.GetInt("siteid",-1);//站点ID
    protected string tPath = PageRequest.GetString("tPath");
    protected int oldFileId = PageRequest.GetInt("fileid",-1);//模板ID
    protected string XwTitles = PageRequest.GetString("xwtitle");
    protected string NodeIds = PageRequest.GetString("Nodeids");//替换的参数
    protected string name = PageRequest.GetString("indexname");
    protected string ColumnName = PageRequest.GetString("ColumnName");//栏目名称
    protected string CommentTemplate = PageRequest.GetString("CommentTemplate");//评论模板
    protected string ContentDefaultTemplateFile = PageRequest.GetString("ContentDefaultTemplateFile");//内容模板
    int FID;
    
    protected KeRui.Model.CmsNode modelCmsNode = new KeRui.Model.CmsNode();
   
    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
    protected KeRui.BLL.CmsTemplate BLLCmsTemplate = new KeRui.BLL.CmsTemplate();
    protected KeRui.BLL.CmsTemplateFile BLLCmsTemplateFile = new KeRui.BLL.CmsTemplateFile();
    protected KeRui.BLL.CmsTemplateFileAttr BLLCmsTemplateFileAttr = new KeRui.BLL.CmsTemplateFileAttr();
    protected KeRui.BLL.CmsTemplateSubjectsAttr BLLCmsTemplateSubjectsAttr = new KeRui.BLL.CmsTemplateSubjectsAttr();
    protected KeRui.BLL.CmsSite BLLCmsSite = new KeRui.BLL.CmsSite();

    string tempaltePath = "";
    private string act = PageRequest.GetString("act");
    //用户模板记录表使用的数据
    int SubjectsFileID = 0;
    int SubjectsAttrID = PageRequest.GetInt("ID", -1);//模板附加ID
    int SubjectsNodeID = 0;//节点
    string  SubjectsNotes = "";

    int nodeid = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        //判断登录和检查权限
        ManageHelper.CheckAdminLogin();
        if (act == "add")
        {
            ManageHelper.CheckAdminPower("site_addsubject_" + siteid);//权限
            //读取源模板
            
            KeRui.Model.CmsTemplateFile ModelCmsTemplateFile = new KeRui.Model.CmsTemplateFile();
            KeRui.Model.CmsTemplateFile NewTemplateFile = new KeRui.Model.CmsTemplateFile();
            //站点ID去查询TemplateID
            ModelCmsTemplateFile = BLLCmsTemplateFile.GetModel(oldFileId);
            string HTML = ModelCmsTemplateFile.FileContent;
            //替换代码
            HTML = replaceNodes(HTML);
            //保存模板
            NewTemplateFile.TemplateID = BLLCmsSite.GetModel(siteid).TemplateID;
            NewTemplateFile.FileName = "Subjects_" + name;
            NewTemplateFile.FileContent = HTML;
            NewTemplateFile.IsDefault = 1;
            NewTemplateFile.IsTop = 0;
            NewTemplateFile.FileType = 8;
            NewTemplateFile.FileUpdateInterval = 0;
            if (BLLCmsTemplateFile.GetList(" [FileName]='" + NewTemplateFile.FileName + "'").Count > 0)
            {
                KeRui.BLL.CmsTemplateFileAttr BLLCmsTemplateFileAttr = new KeRui.BLL.CmsTemplateFileAttr();
                Response.Redirect("NewSubject.aspx?siteid=" + siteid + "&FileID=" + oldFileId + "&ID=" + BLLCmsTemplateFileAttr.GetList(" FileID='" + oldFileId + "'", 1, 1)[0].ID + "&errmsg=发现同名模板文件，请更改索引名！");
            }
            FID = BLLCmsTemplateFile.Add(NewTemplateFile);
            ManageHelper.AddLog("site_addsubject_" + siteid, "添加了“" + NewTemplateFile.FileName + "”模板文件。");

            nodeid = Add();//添加栏目
            KeRui.Model.CmsTemplateFile newfile = new KeRui.Model.CmsTemplateFile();
            newfile = BLLCmsTemplateFile.GetModel(FID);
            newfile.FileContent = newfile.FileContent.Replace("{nodeid}", nodeid.ToString());
            BLLCmsTemplateFile.Update(newfile);


            SubjectsFileID = FID;
            //KeRui.BLL.CmsTemplate BLLCmsTemplate = new KeRui.BLL.CmsTemplate();
            //KeRui.Model.CmsTemplate ModelCmsTemplate = new KeRui.Model.CmsTemplate();
            //ModelCmsTemplate = BLLCmsTemplate.GetModel(newfile.TemplateID);
            FileObj.WriteFileNoSign(newfile.FilePath, newfile.FileContent);

            AddSubjects();
        }
        if (act == "editsave")
        {
            ManageHelper.CheckAdminPower("site_updatesubject_" + siteid);//权限
            //获源模板
            int AttrID = PageRequest.GetInt("ID", -1);
            int FileID=PageRequest.GetInt("fileid",-1);

            KeRui.Model.CmsTemplate ModelCmsTemplate = new KeRui.Model.CmsTemplate();
            KeRui.Model.CmsTemplateFile ModelCmsTemplateFile = new KeRui.Model.CmsTemplateFile();
            KeRui.Model.CmsTemplateFileAttr ModelCmsTemplateFileAttr = new KeRui.Model.CmsTemplateFileAttr();
            KeRui.Model.CmsTemplateSubjectsAttr ModelCmsTemplateSubjectsAttr = new KeRui.Model.CmsTemplateSubjectsAttr();

            ModelCmsTemplateFileAttr = BLLCmsTemplateFileAttr.GetModel(AttrID);
            
            string HTML = BLLCmsTemplateFile.GetModel(ModelCmsTemplateFileAttr.FileID).FileContent;
            HTML = replaceNodes(HTML);
            //修改模板附加信息
            ModelCmsTemplateSubjectsAttr = BLLCmsTemplateSubjectsAttr.GetList(" FileID='" + FileID + "'")[0];
            ModelCmsTemplateSubjectsAttr.Notes = SubjectsNotes;
            BLLCmsTemplateSubjectsAttr.Update(ModelCmsTemplateSubjectsAttr);

            HTML = HTML.Replace("{nodeid}", ModelCmsTemplateSubjectsAttr.NodeID.ToString());
            //修改模板内容
            ModelCmsTemplateFile = BLLCmsTemplateFile.GetModel(FileID);
            ModelCmsTemplateFile.FileContent = HTML;
            BLLCmsTemplateFile.Update(ModelCmsTemplateFile);
            ModelCmsTemplate = BLLCmsTemplate.GetModel(ModelCmsTemplateFile.TemplateID);
            FileObj.WriteFileNoSign(ModelCmsTemplate.TemplateDir + "/" + ModelCmsTemplate.TemplateFolder + "/" + ModelCmsTemplateFile.FileName , ModelCmsTemplateFile.FileContent);
            //修改栏目内容和评论模板
            KeRui.Model.CmsNode ModelCmsNode = bllCmsNode.GetModel(ModelCmsTemplateSubjectsAttr.NodeID);
            ModelCmsNode.CommentTemplate = CommentTemplate;
            ModelCmsNode.ContentDefaultTemplateFile = ContentDefaultTemplateFile;
            bllCmsNode.Update(ModelCmsNode);
            //生成页面
            Publish publishContent = new Publish();
            publishContent.PublishNodePage(ModelCmsTemplateSubjectsAttr.NodeID);
            ManageHelper.AddLog("site_updatesubject_" + siteid, "修改了“" + ModelCmsTemplateFile.FileName + "”模板文件。");
            Response.Write("<script type=\"text/javascript\">alert('修改成功！'); location.href='subjectList.aspx?siteid=" + siteid + "'</script>");
        }
        
    }
    
    //替换,
    private string replaceNodes(string html)
    {

        html = html.Replace("{PageTitle}", ColumnName + "——专题");
        
        string[] tPaths = Utils.SplitString(tPath, ",");
        for (int j = 0; j < tPaths.Length; j++)
        {
            if (tPaths[j] != "")
            {
                html = html.Replace("{TP" + j + "}", tPaths[j]);
                SubjectsNotes = SubjectsNotes + tPaths[j] + ",";
            }
        }
        SubjectsNotes = SubjectsNotes + "_,";
        string[] nodeIds = Utils.SplitString(NodeIds, ",");
        for (int i = 0; i < nodeIds.Length; i++)
        {
            if (nodeIds[i] != "")
            {
                html = html.Replace("{nodeXW" + i + "}", "node" + nodeIds[i]);
                SubjectsNotes = SubjectsNotes + nodeIds[i] + ",";
            }
        }
        SubjectsNotes = SubjectsNotes + "_,";
        string[] xwtitles = Utils.SplitString(XwTitles, ",");
        for (int i = 0; i < xwtitles.Length; i++)
        {
            if (xwtitles[i] == "")
            {
                xwtitles[i] = "未命名";
            }
                html = html.Replace("{xTitle" + i + "}", xwtitles[i]);
                SubjectsNotes = SubjectsNotes + xwtitles[i] + ",";
            
        }

            return html;
    }
    private int Add()
    {
        ManageHelper.CheckAdminPower("site_nodeadd_" + siteid);//权限
       
        KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();
        modelCmsSite = BLLCmsSite.GetModel(siteid);
        modelCmsNode.ParentID = (bllCmsNode.GetList("SiteID='" + siteid + "' and NodeKey='subject' and Depth='1'"))[0].NodeID;
        modelCmsNode.NodeName = ColumnName;
        modelCmsNode.NodeKey = "Subjects_" + name;
        modelCmsNode.NodeType = 2;
        modelCmsNode.ContentDefaultTemplateFile = ContentDefaultTemplateFile;
        modelCmsNode.TemplateFile = GetPath();
        modelCmsNode.ModelID = 0;
        modelCmsNode.NodeLinkUrl = "";
        modelCmsNode.CommentTemplate = CommentTemplate;
        modelCmsNode.ContentPageSize = 45;
        modelCmsNode.NodePicUrl = PageRequest.GetString("NodePicUrl");//须处理
        modelCmsNode.MetaKeywords = PageRequest.GetString("MetaKeywords");
        modelCmsNode.MetaDescription = PageRequest.GetString("MetaDescription");
        modelCmsNode.ShowOnMenu = PageRequest.GetInt("ShowOnMenu", 0) == 1 ? true : false;
        KeRui.Model.CmsNode PmodelCmsNode = new KeRui.Model.CmsNode();
        PmodelCmsNode = bllCmsNode.GetModel(modelCmsNode.ParentID);
        if (PmodelCmsNode.ParentID != 0)
        {
            modelCmsNode.NodePath = PmodelCmsNode.NodePath + @"\" + modelCmsNode.NodeKey;
            modelCmsNode.NodeDir = PmodelCmsNode.NodeDir + @"\" + modelCmsNode.NodeKey;
        }
        else
        {
            modelCmsNode.NodePath = modelCmsNode.NodeKey;
            modelCmsNode.NodeDir = modelCmsSite.SiteDir + @"\" + modelCmsNode.NodeKey;
        }
        modelCmsNode.NodePath = modelCmsNode.NodePath.Replace(@"\", "/");
        modelCmsNode.SiteID = siteid;
        modelCmsNode.Description = "";
        modelCmsNode.Tips = "";
        KeRui.BLL.Account bllAccount = new KeRui.BLL.Account();
        modelCmsNode.CreateUser = bllAccount.GetModel(KeRui.BLL.Account.GetLoginUserID()).Username;
        int NodeID = -1;
        try
        {
            if (PmodelCmsNode.ParentID != 0)
            {
                FileObj.FolderCreate(PmodelCmsNode.NodeDir, modelCmsNode.NodeKey);
            }
            else
            {
                FileObj.FolderCreate(modelCmsSite.SiteDir, modelCmsNode.NodeKey);
            }
            NodeID = bllCmsNode.Add(modelCmsNode);
        }
        catch (Exception ex)
        {
            nsb.AddErr(ex.ToString()); nsb.ChkErr();
        }
        SubjectsNodeID = NodeID;
        modelCmsNode = bllCmsNode.GetModel(NodeID);
        ManageHelper.AddLog("site_nodeadd_" + siteid, "添加了“" + modelCmsNode.NodeName + "”专题栏目。");
        return modelCmsNode.NodeID;
    }
    public string GetPath()
    {
       
        KeRui.Model.CmsTemplateFile modelCmsTemplateFile = new KeRui.Model.CmsTemplateFile();
        modelCmsTemplateFile=BLLCmsTemplateFile.GetModel(FID);
        return modelCmsTemplateFile.FilePath;
    }
    private void AddSubjects()
    {
        
        KeRui.Model.CmsTemplateSubjectsAttr ModelCmsTemplateSubjectsAttr = new KeRui.Model.CmsTemplateSubjectsAttr();
        ModelCmsTemplateSubjectsAttr.FileID = SubjectsFileID;
        ModelCmsTemplateSubjectsAttr.NodeID = SubjectsNodeID;
        ModelCmsTemplateSubjectsAttr.AttrID = SubjectsAttrID;
        ModelCmsTemplateSubjectsAttr.Notes = SubjectsNotes;
        BLLCmsTemplateSubjectsAttr.Add(ModelCmsTemplateSubjectsAttr);
        //生成页面
        Publish publishContent = new Publish();
        publishContent.PublishNodePage(SubjectsNodeID);
        //系统日志
       
        Response.Write("<script type=\"text/javascript\">alert('添加成功！'); location.href='subjectList.aspx?siteid=" + siteid + "'</script>");
        
    }
}
